<template>
  <div class="login">
      <div class="info">
            <input type="text" class="name" placeholder="name" v-model="name" name="">
            <input @keyup.enter="login" type="password" class="password" placeholder="password" v-model="password" name="">
            <button  @click="login">登录</button>
      </div>
  </div>
</template>
<script>
import axios from 'axios'
import { Login } from '@/config/api'
export default {
  name: 'login',
  data() {
    return {
      name: '',
      password:''
    }
  },
  methods:{
    async login(){
        let name=this.name
        let password=this.password
        let that=this
        if(name!=''&&password!=''){
            let date={
                "grant_type": "password",
                "client_id": "admin",
                "client_secret": "8BAk3G10$2EWnN9Frg0SdOIovjQXXGS91gj3waZn",
                "scope": "admin",
                "username": this.name,
                "password": this.password
            };
            await Login('/token',date).then(function(reg){

              let token=that.jsEncrypt(reg.data.access_token)
              let d = new Date();
              d.setTime(d.getTime() + (604800000));
              let expires = "expires=" + d.toUTCString();
              document.cookie = "admin" + "=" + token + "; " + expires;
              that.$store.dispatch("tokensync",reg.data.access_token);
              //that.$router.push('/home')
              //window.location.reload();
            }).catch(function(err){
              alert(err.data.message)
            })
            

        }else{
            alert('请输入账号密码!')
        }
        
    }
  }
}

</script>

